package com.maibow.service.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.maibow.constants.SiteCachedKey;
import com.maibow.dao.DaoSupport;
import com.maibow.entity.Page;
import com.maibow.entity.site.Area;
import com.maibow.entity.site.City;
import com.maibow.entity.site.Province;
import com.maibow.util.PageData;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.service.organ
 * 文件名：OrganService.java
 * 作者: 吴比(Wubi)
 * 时间：2015年4月7日-上午9:15:13
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@Service("siteService")
public class SiteService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    /**
     * 
     * queryAllAreaList(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pageData
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：List<Area>
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    @SuppressWarnings("unchecked")
    public List<Area> queryAllAreaListByCityCode(PageData pageData) throws Exception {
        String citycode = pageData.getString("citycode");
        BoundValueOperations<String, List<Area>> boundValueOperations = redisTemplate.boundValueOps(SiteCachedKey.SITE_AREA_PREFIX_KEY + citycode);
        List<Area> areaList = boundValueOperations.get();
        if (areaList == null || areaList.size() == 0 || areaList.get(0).getAreacode() == null) {
            try {
                areaList = (List<Area>) dao.findForList("SiteMapper.queryAllAreaListByCityCode", pageData);
            } catch (Exception e) {
                // TODO Auto-generated catch
                // block
                e.printStackTrace();
            }

            if (null != areaList) {
                boundValueOperations.set(areaList);
            }
        }

        return areaList;
    }

    /**
     * 
     * queryAllCityListByProvinceCode(
     * 这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pageData
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：List<Area>
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public List<City> queryAllCityListByProvinceCode(PageData pageData) throws Exception {
        String provincecode = pageData.getString("provincecode");
        BoundValueOperations<String, List<City>> boundValueOperations = redisTemplate.boundValueOps(SiteCachedKey.SITE_CITY_PREFIX_KEY + provincecode);
        List<City> cityList = boundValueOperations.get();
        if (cityList == null || cityList.size() == 0 || cityList.get(0).getCitycode() == null) {
            try {
                cityList = (List<City>) dao.findForList("SiteMapper.queryAllCityListByProvinceCode", pageData);
            } catch (Exception e) {
                // TODO Auto-generated catch
                // block
                e.printStackTrace();
            }

            if (null != cityList) {
                boundValueOperations.set(cityList);
            }
        }

        return cityList;
    }

    /**
     * 
     * queryAllProvinceList(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param pageData
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：List<Province>
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public List<Province> queryAllProvinceList(PageData pageData) throws Exception {
        BoundValueOperations<String, List<Province>> boundValueOperations = redisTemplate.boundValueOps(SiteCachedKey.SITE_PROVINCE_PREFIX_KEY);
        List<Province> provinceList = boundValueOperations.get();
        if (provinceList == null || provinceList.size() == 0 || provinceList.get(0).getProvincecode() == null) {
            try {
                provinceList = (List<Province>) dao.findForList("SiteMapper.queryAllProvinceList", pageData);
            } catch (Exception e) {
                // TODO Auto-generated catch
                // block
                e.printStackTrace();
            }

            if (null != provinceList) {
                boundValueOperations.set(provinceList);
            }
        }

        return provinceList;
    }

    /**
     * 
     * getProvinceInfoByCode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param provincecode
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：Province
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public Province getProvinceInfoByCode(String provincecode) throws Exception {
        BoundValueOperations<String, Province> boundValueOperation = redisTemplate.boundValueOps(SiteCachedKey.SITE_PROVINCE_CODE_PREFIX_KEY + provincecode);
        Province province = boundValueOperation.get();
        if (province == null || province.getProvincecode() == null) {
            province = (Province) dao.findForObject("SiteMapper.getProvinceInfoByCode", provincecode);

            if (null != province) {
                boundValueOperation.set(province);
            }
        }

        return province;
    }

    /**
     * 
     * getCityInfoByCode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param citycode
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：City
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public City getCityInfoByCode(String citycode) throws Exception {
        BoundValueOperations<String, City> boundValueOperation = redisTemplate.boundValueOps(SiteCachedKey.SITE_CITY_CODE_PREFIX_KEY + citycode);
        City city = boundValueOperation.get();
        if (city == null || city.getCitycode() == null) {
            city = (City) dao.findForObject("SiteMapper.getCityInfoByCode", citycode);

            if (city != null) {
                boundValueOperation.set(city);
            }
        }

        return city;
    }

    /**
     * 
     * getAreaInfoByCode(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param areacode
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：Area
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public Area getAreaInfoByCode(String areacode) throws Exception {
        BoundValueOperations<String, Area> boundValueOperation = redisTemplate.boundValueOps(SiteCachedKey.SITE_AREA_CODE_PREFIX_KEY + areacode);
        Area area = boundValueOperation.get();
        if (area == null || area.getAreacode() == null) {
            area = (Area) dao.findForObject("SiteMapper.getAreaInfoByCode", areacode);

            if (null != area) {
                boundValueOperation.set(area);
            }
        }

        return area;
    }

    /**
     * 
     * queryAllCityList(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@param page
     * 标记：@return
     * 标记：@throws Exception
     * 返回值：List<City>
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public List<PageData> queryAllCitylistPage(Page page) throws Exception {
        List<PageData> provinceList = (List<PageData>) dao.findForList("SiteMapper.queryAllCitylistPage", page);
        return provinceList;
    }
}